package com.sky.controller.admin;

import com.sky.dto.NonHeritageDTO;
import com.sky.dto.NonHeritagePageDTO;
import com.sky.entity.Nonheritage;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.InheritorsService;
import com.sky.service.NonheritageImgService;
import com.sky.service.NonheritageService;
import com.sky.service.PanoramaService;
import com.sky.vo.NonHeritagePageVO;
import com.sky.vo.NonHeritageVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/nonHeritage")
@Api(tags = "管理端非遗信息相关接口")
public class NonHeritageController {

    @Autowired
    private NonheritageService nonHeritageService;

    @Autowired
    private InheritorsService inheritorsService;

    @Autowired
    private NonheritageImgService nonHeritageImgService;

    @Autowired
    private PanoramaService panoramaService;

    /**
     * 新增/修改 非遗信息
     */
    @PostMapping
    public Result<String> addNonHeritage(@RequestBody NonHeritageDTO nonHeritage) {
        boolean bool =  nonHeritageService.saveOrUpdate(nonHeritage);
        return bool? Result.success():Result.error("错误");
    }

    /**
     * 删除非遗信息
     */
    @DeleteMapping
    public Result<String> delete(@RequestParam List<String> ids) {
        boolean bool = nonHeritageService.removeByIds(ids);
        return bool ? Result.success():Result.error("错误");
    }

    /**
     * 根据id查询指定非遗信息
     */
    @GetMapping("/{id}")
    public Result<NonHeritageVO> GetNonHeritage(@PathVariable Integer id){
        NonHeritageVO nonHeritageVO = new NonHeritageVO();
        Nonheritage nonHeritage = nonHeritageService.getById(id);
        BeanUtils.copyProperties(nonHeritage,nonHeritageVO);

        nonHeritageVO.setInheritors(inheritorsService.getByNonheritage(id));
        nonHeritageVO.setNonheritageImgs(nonHeritageImgService.getByNonheritage(id));
        nonHeritageVO.setPanoramas(panoramaService.getByNonheritage(id));
        return Result.success(nonHeritageVO);
    }

    /**
     * 分页查询非遗信息
     */
    @GetMapping("/page")
    public Result<PageResult<NonHeritagePageVO>> pageQuery(NonHeritagePageDTO nonHeritagePageDTO){
        NonHeritagePageVO nonHeritagePageVO= nonHeritageService.pageQuery(nonHeritagePageDTO);
        return Result.success();
    }
}
